/*
 by txdnet.cn tonydon 
 */
package txdnet.util.mail;

import java.util.Date;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import txdnet.util.Logger;

/**
 * 发送邮件类
 * <pre>
 *        SmtpParam param = new SmtpParam();
 * param.setHost("smtp.163.com");
 * param.setFromMail("txdnetmailer@163.com");
 * param.setEnableSSL(false);
 * param.setPort("25");
 * param.setLoginMail("txdnetmailer@163.com");
 * param.setLoginPass("I3vHgd30");
 * param.setMailerCode("Mailer-001");
 * param.initConfig();
 *
 * SmtpMailer.sendHtmlMail(
 * "372578374@qq.com", "uobai", "javamail测试邮件(重要)",
 * "<html><body><b>javamail测试邮件.请重置密码ddd.</b></body></html>",
 * param);
 * </pre>
 *
 * @author txdnet
 */
public final class SmtpMailer {

    private SmtpMailer() {
    }

    public static enum Type {

        TEXT,
        HTML
    };

    /*
     * 发送HTML格式邮件
     */
    public static boolean sendHtmlMail(String tomail, String personal, String subject, String content,
            final SmtpParam smtpParam) {
        return SmtpMailer.sendMail(tomail, personal, subject, content, smtpParam,
                SmtpMailer.Type.HTML);
    }

    /*
     * 发送TEXT格式邮件
     */
    public static boolean sendTextMail(String tomail, String personal, String subject, String content,
            final SmtpParam smtpParam) {
        return SmtpMailer.sendMail(tomail, personal, subject, content, smtpParam,
                SmtpMailer.Type.TEXT);
    }

    /*
     * 可以发送附件邮件
     */
    public static boolean sendMail(String tomail, String personal, String subject, String content,
            final SmtpParam smtpParam, SmtpMailer.Type type, String... files) {

        boolean flag = true;
        try {
            Session sn = smtpParam.getSession();
            MimeMessage msg = new MailMessage(sn, smtpParam.getMailerCode());
            msg.setFrom(new InternetAddress(smtpParam.getFromMail(), personal));
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(tomail, false));
            msg.setSubject(subject, smtpParam.getCharset());
            msg.setSentDate(new Date());
            BodyPart mdp = new MimeBodyPart();
            if (type == SmtpMailer.Type.HTML) {
                // 发送HTML格式邮件
                mdp.setContent(content, "text/html;charset=".concat(smtpParam.getCharset()));
            } else if (type == SmtpMailer.Type.TEXT) {
                //发送文本格式邮件
                mdp.setText(content);
            } else {
                throw new RuntimeException("Mail Type Error. SmtpParam.mailerCode=".concat(smtpParam.getMailerCode()));
            }
            if (smtpParam.isEnableBase64()) {
                mdp.setHeader("Content-Transfer-Encoding", "base64");
            }
            Multipart mm = new MimeMultipart();
            mm.addBodyPart(mdp);
            if (files != null && files.length > 0) {
                MimeBodyPart mbpfile;
                for (String file : files) {
                    mbpfile = new MimeBodyPart();
                    mbpfile.attachFile(file);
                    mm.addBodyPart(mbpfile);
                }
            }
            msg.setContent(mm);
            msg.saveChanges();
            // Transport t=sn.getTransport(ssl ? "smtps" : "smtp");
            // t.connect(mailhost, frommail , frommailpk); 
            // t.sendMessage(msg,  msg.getAllRecipients());  
            Transport.send(msg);
            // t.close();
        } catch (Exception ex) {
            Logger.error(SmtpMailer.class, "SmtpParam.mailerCode=".concat(smtpParam.getMailerCode()), ex);
            flag = false;
        }
        return flag;
    }
//   
//   
///* 该方法被废除
// * @see Apache Common Email
// */
//   public static boolean sendMail(
//           String mailhost,
//           String sslsmtpport,
//           String smtpport,
//           boolean tlsenble,
//           boolean sslenble,
//           String frommail,
//           String username,
//           String mailpk,
//           String tomail,
//           String toname,
//           String sender,
//           String subject,
//           String content,
//           int socketTimeout,
//           int socketConnectTimeout
//           ){
//       boolean flag = true;
//           try{
//                HtmlEmail email = new HtmlEmail();
//                email.setTLS(tlsenble); 
//                email.setSSL(sslenble);
//                if(sslenble)
//                {
//                    email.setSslSmtpPort(sslsmtpport);
//                }else{
//                    email.setSmtpPort(Integer.parseInt(smtpport));
//                }
//                email.setHostName(mailhost); //发送邮件服务器
//                email.setFrom(frommail, sender);
//                email.setAuthentication(username, mailpk); //你的账号密码
//                email.addTo(tomail, toname); //接收方
//                email.setCharset(StaticMark.STRING_ENCODING);
//                email.setSubject(subject); //标题
//                email.setHtmlMsg(content);
//                email.setSocketTimeout(socketTimeout);
//                email.setSocketConnectionTimeout(socketConnectTimeout);
//                email.send();
//                }catch(Exception ex){
//                     Logger.info(SmtpMailer.class, ex.toString());
//                     flag = false;
//                 }
//               return flag;
//   }
//   
}